package cn.db101.view;

import cn.db101.dao.adminDao;
import cn.db101.model.reader;
import cn.db101.util.DbUtil;
import cn.db101.util.StringUtil;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class add_limit_Frame{
	 JFrame jf=new JFrame() ;
	private JTextField textField_1;
	private DbUtil dbUtil=new DbUtil();

	

	/**
	 * Create the frame.
	 * @wbp.parser.entryPoint
	 */
	public void init() {
		jf.setTitle("\u6DFB\u52A0\u6743\u9650");
		jf.setBounds(100, 100, 505, 483);
		
		JPanel panel = new JPanel();
		jf.getContentPane().add(panel, BorderLayout.CENTER);
		
		JPanel panel_1 = new JPanel();
		panel_1.setToolTipText("");
		panel_1.setForeground(new Color(0, 0, 0));
		panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u6DFB\u52A0\u6743\u9650", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(add_limit_Frame.class.getResource("/img/x.jpg")));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addComponent(lblNewLabel)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(27)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 442, GroupLayout.PREFERRED_SIZE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 258, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 159, Short.MAX_VALUE)
					.addContainerGap())
		);
		
		JLabel label = new JLabel("\u8BF7\u8F93\u5165\u8D26\u53F7\uFF1A");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		JButton button = new JButton("\u786E\u5B9A");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addlimit();
			}
		});
		button.setIcon(new ImageIcon(add_limit_Frame.class.getResource("/icon/\u767B\u5F55-\u7528\u6237\u540D.png")));
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGap(59)
							.addComponent(label)
							.addGap(35)
							.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGap(139)
							.addComponent(button)))
					.addContainerGap(100, Short.MAX_VALUE))
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGap(25)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(40)
					.addComponent(button, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
					.addGap(105))
		);
		panel_1.setLayout(gl_panel_1);
		panel.setLayout(gl_panel);
		//���ô����Ƿ�ɸı�
		jf.setResizable(false);
		//���ô���ɼ�
		jf.setVisible(true);	
		//����JFrame������ʾ
		jf.setLocationRelativeTo(null);

	}
	public void addlimit(){
			String reader_no=new String(this.textField_1.getText());
			if(StringUtil.isEmpty(reader_no)) {
				JOptionPane.showMessageDialog(null,"�˺Ų���Ϊ�գ�");
				return;
			}
			Connection con=null;
			try {
				con = dbUtil.getCon();
				String sql="select password,reader_name,gender from reader where reader_no=?";
				PreparedStatement pstmt=con.prepareStatement(sql);
				pstmt.setString(1, reader_no);
				ResultSet rs=pstmt.executeQuery();
				if(rs.next()) {
					String password= rs.getString("password");	
					String reader_name = rs.getString("reader_name");	
					String gender = rs.getString("gender");	
					reader rea = new reader(reader_no,password,reader_name,gender);
					int registerNum= adminDao.limit(con, rea);
					if(registerNum==1) {
						JOptionPane.showMessageDialog(null, "���Ȩ�޳ɹ���");
					}else {
						JOptionPane.showMessageDialog(null, "���Ȩ��ʧ�ܣ�");
					}
				}
			}catch(Exception e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null, "���Ȩ��ʧ�ܣ�");
			}finally {
				try {
					dbUtil.closeCon(con);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		
	}
}
